var host = "http://www.owin.cc/"
var ajaxUrlObj = {
  'GET_INDEX_BANNER':host+"index_get_banner",
  'GET_COMPANY_INFO':host+"getCompanyInfo",
  'GET_CASES_LIST':host+"getCasesListByWeb",
  'GET_PROJECT_BANNER':host+"get_project_banner",
  'GET_ABOUT_INFO':host+"getAboutInfo",
  'GET_NEWS_LIST':host+"getInformationList",
  'POST_MESSAGE':host+"addCustomerMessage",
  'GET_NEW_DETAIL':host+"getInformation",
  'GET_PRODUCT_LIST':host+"getProductList",
  'GET_CASES_DETAIL':host+"getCases",
  'GET_POST_CODE':host+"getImageCode",
  'GET_SEO':host+"getSeo",
  'GET_PRODUCT_DETAIL':host+"getProduct",
  'GET_SEARCH_LIST':host+"get_list_by_keyword",
  'GET_TREE':host+"getTree"
}
var cacheStorage = JSON.parse(localStorage.getItem("cacheStorageM"))
if (!cacheStorage) {
  cacheStorage = {}
}
function fetchApi (config) {
  var type = config.type;
  var apiKey = config.apiKey;
  var postData = config.postData;
  var successCb = config.successCb;
  var errorCb = config.errorCb;
  var nowDate = new Date().getTime()
  if (!cacheStorage[apiKey]) {
    cacheStorage[apiKey] = {}
  }
  var cacheData = cacheStorage[apiKey]
  if (cacheData.date && nowDate - cacheData.date < 2*60*60*1000){
    successCb && successCb(cacheData.data)
  } else {
    $.ajax({
      type: type,
      url: ajaxUrlObj[apiKey],
      data: postData,
      success: function(rep){
        if (typeof rep === 'string') {
          rep = JSON.parse(rep)
        }
        if (rep.code == 1){
          if (rep.data === 'null') return
          var data = rep.data ? rep.data : rep
          data = typeof data === 'string' ? JSON.parse(data) : data
          if(data.rows&&typeof data.rows === 'string'){
            data.rows = JSON.parse(data.rows)
          }
          cacheData.data = data
          cacheData.date = nowDate
          localStorage.setItem("cacheStorage", JSON.stringify(cacheStorage))
          successCb && successCb(data)
        }
      },
      error: function(error){
        console.log(error)
        errorCb && errorCb(error)
      }
    });
  }
}
function setNavBar () {
  fetchApi({
    type:"GET",
    apiKey:'GET_TREE',
    successCb:function(data){
      var nav = data
      var navStr = ''
      for(var i =0,l=nav.length;i<l;i++){
        navStr+='<a href="products.html?type='+nav[i].id+'" class=item>'+nav[i].text+'</a>'
      }
      var str = '<img src=img/logo.png class=logo> <div class=m-btn> <span class=box> <span class="line line1"></span> <span class="line line2"></span> <span class="line line3"></span> </span>导航</div> <div class=menu-box> <div class=top-bar> <span>MENU</span> </div> <div class=nav-item> <a href="index.html" class=item>首页</a> <a href="aboutus.html " class=item>我们</a> <a href="news.html " class=item>新闻</a> <div class="item menu-item">产品 <div class="child-item hide">'+navStr+'</div> </div> <a href="projects.html" class=item>案例</a> <a href="msg.html " class=item>留言</a> <a href="contact.html " class=item>联系</a> </div> </div>'
      $("#publicNav").html(str)
      intEvent()
    }
  })
  
}
function setToolBar () {
  var str = '<a class=item><div class="font iconE"><i class=iconfont></i><span class="p_toolname ">首页</span></div></a><a class=item href=tel:020-38343771><div class="font iconD"><i class=iconfont></i><span class="p_toolname">电话</span></div></a>'
  $("#publicTool").html(str)
}
function getSeoInfo (obj) {
  var id= obj.id;
  var name = obj.name;
  if(id) {
    return
  } else if(name) {
    fetchApi({
      type:"POST",
      apiKey:'GET_SEO',
      successCb:function(data){
        $("title").html(data.title)
        $('meta[name="keywords"]').attr("content",data.keyword)
        $('meta[name="description"]').attr("content",data.descs)
      }
    })
  }
}
function getQueryString(name) { 
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
  var r = window.location.search.substr(1).match(reg); 
  if (r != null) return r[2]; 
  return null; 
}
function intEvent() {
  $("#publicNav .m-btn").on("click",function(){
    console.log(111)
    let $nav = $("#publicNav")
    $nav.hasClass("menu-active") ? $nav.removeClass("menu-active") : $nav.addClass("menu-active")
  })
  $('.menu-box').on('touchmove',function(e) {
    e.preventDefault();
  })
  $("#publicNav .menu-item").on("click",function(){
    var $this=$(this)
    var $child = $this.find(".child-item")
    $child.hasClass("hide") ? $child.removeClass("hide") : $child.addClass("hide")
  })
}
if (window.screen.width > 640 ) {
  window.location.href = 'http://www.owin.cc/mobile/index.html'
}
$(window).ready(function(){
  setNavBar()
  setToolBar()
  var seoObj = {}
  var id = getQueryString('id')
  if (id) {
    seoObj.id = id
  } else {
    seoObj.name="index"
  }
  getSeoInfo(seoObj)
})